<html>
<head><meta charset="utf-8"><title>add a function that is not used in source code · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/add.20a.20function.20that.20is.20not.20used.20in.20source.20code.html">add a function that is not used in source code</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="220669030"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/add%20a%20function%20that%20is%20not%20used%20in%20source%20code/near/220669030" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> sfzhu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/add.20a.20function.20that.20is.20not.20used.20in.20source.20code.html#220669030">(Dec 22 2020 at 08:40)</a>:</h4>
<p>I am doing some experimental changes on the compiler for a project. In some circmustances that generate llvm instruction <code>alloca</code>, I hope it can be turned into a heap/global allocation. Therefore, I may need a function call like <a href="https://stdrs.dev/nightly/x86_64-pc-windows-gnu/alloc/alloc/fn.exchange_malloc.html">https://stdrs.dev/nightly/x86_64-pc-windows-gnu/alloc/alloc/fn.exchange_malloc.html</a> in the evetual LLVM code, while the source code and MIR don't have such a function call. How can I do that?</p>



<a name="220669191"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/add%20a%20function%20that%20is%20not%20used%20in%20source%20code/near/220669191" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> sfzhu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/add.20a.20function.20that.20is.20not.20used.20in.20source.20code.html#220669191">(Dec 22 2020 at 08:43)</a>:</h4>
<p>Alternatively, someone explaining how <code>Box::new()</code> is compiled internally is also helpful.</p>



<a name="220695083"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/add%20a%20function%20that%20is%20not%20used%20in%20source%20code/near/220695083" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> sfzhu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/add.20a.20function.20that.20is.20not.20used.20in.20source.20code.html#220695083">(Dec 22 2020 at 14:38)</a>:</h4>
<p>I know <code>Box::new</code> uses <code>box</code> keyword internally, but I didn't find how <code>box</code> keyword is implemented in the compiler...</p>



<a name="220696698"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/add%20a%20function%20that%20is%20not%20used%20in%20source%20code/near/220696698" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/add.20a.20function.20that.20is.20not.20used.20in.20source.20code.html#220696698">(Dec 22 2020 at 14:53)</a>:</h4>
<p><span class="user-mention" data-user-id="343499">@sfzhu</span> you could look for code modifying <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/enum.ExprKind.html#variant.Box">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_hir/enum.ExprKind.html#variant.Box</a></p>



<a name="220696786"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/add%20a%20function%20that%20is%20not%20used%20in%20source%20code/near/220696786" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/add.20a.20function.20that.20is.20not.20used.20in.20source.20code.html#220696786">(Dec 22 2020 at 14:54)</a>:</h4>
<p><code>box</code> has special support in MIR too</p>



<a name="220696899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/add%20a%20function%20that%20is%20not%20used%20in%20source%20code/near/220696899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/add.20a.20function.20that.20is.20not.20used.20in.20source.20code.html#220696899">(Dec 22 2020 at 14:55)</a>:</h4>
<p>It is internally implemented as emitting a MIR statement that codegens to a call to the <code>exchange_malloc</code> lang item and then writing the given value to the memory pointed to by the result of <code>exchange_malloc</code>.</p>



<a name="220697091"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/add%20a%20function%20that%20is%20not%20used%20in%20source%20code/near/220697091" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/add.20a.20function.20that.20is.20not.20used.20in.20source.20code.html#220697091">(Dec 22 2020 at 14:57)</a>:</h4>
<p>Cunningham's Law strikes again <span aria-label="laughing" class="emoji emoji-1f606" role="img" title="laughing">:laughing:</span></p>



<a name="220757756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/add%20a%20function%20that%20is%20not%20used%20in%20source%20code/near/220757756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> sfzhu <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/add.20a.20function.20that.20is.20not.20used.20in.20source.20code.html#220757756">(Dec 23 2020 at 03:12)</a>:</h4>
<p><span class="user-mention silent" data-user-id="133247">bjorn3</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/add.20a.20function.20that.20is.20not.20used.20in.20source.20code/near/220696899">said</a>:</p>
<blockquote>
<p>It is internally implemented as emitting a MIR statement that codegens to a call to the <code>exchange_malloc</code> lang item and then writing the given value to the memory pointed to by the result of <code>exchange_malloc</code>.</p>
</blockquote>
<p>Hello, would you mind if telling me where such codegen happens? Many thanks</p>



<a name="220758051"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/add%20a%20function%20that%20is%20not%20used%20in%20source%20code/near/220758051" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Noah Lev <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/add.20a.20function.20that.20is.20not.20used.20in.20source.20code.html#220758051">(Dec 23 2020 at 03:21)</a>:</h4>
<p><span class="user-mention" data-user-id="343499">@sfzhu</span> Looks like it happens <a href="https://github.com/rust-lang/rust/blob/969b42d8c0e44c6b895ab4582b5ae0a0ce319fdf/compiler/rustc_codegen_ssa/src/mir/rvalue.rs#L519-L541">here</a>:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="w">            </span><span class="n">mir</span>::<span class="n">Rvalue</span>::<span class="n">NullaryOp</span><span class="p">(</span><span class="n">mir</span>::<span class="n">NullOp</span>::<span class="nb">Box</span><span class="p">,</span><span class="w"> </span><span class="n">content_ty</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">content_ty</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="bp">self</span><span class="p">.</span><span class="n">monomorphize</span><span class="p">(</span><span class="n">content_ty</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">content_layout</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bx</span><span class="p">.</span><span class="n">cx</span><span class="p">().</span><span class="n">layout_of</span><span class="p">(</span><span class="n">content_ty</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">llsize</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bx</span><span class="p">.</span><span class="n">cx</span><span class="p">().</span><span class="n">const_usize</span><span class="p">(</span><span class="n">content_layout</span><span class="p">.</span><span class="n">size</span><span class="p">.</span><span class="n">bytes</span><span class="p">());</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">llalign</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bx</span><span class="p">.</span><span class="n">cx</span><span class="p">().</span><span class="n">const_usize</span><span class="p">(</span><span class="n">content_layout</span><span class="p">.</span><span class="n">align</span><span class="p">.</span><span class="n">abi</span><span class="p">.</span><span class="n">bytes</span><span class="p">());</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">box_layout</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bx</span><span class="p">.</span><span class="n">cx</span><span class="p">().</span><span class="n">layout_of</span><span class="p">(</span><span class="n">bx</span><span class="p">.</span><span class="n">tcx</span><span class="p">().</span><span class="n">mk_box</span><span class="p">(</span><span class="n">content_ty</span><span class="p">));</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">llty_ptr</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bx</span><span class="p">.</span><span class="n">cx</span><span class="p">().</span><span class="n">backend_type</span><span class="p">(</span><span class="n">box_layout</span><span class="p">);</span><span class="w"></span>


<span class="w">                </span><span class="c1">// Allocate space:</span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">def_id</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="k">match</span><span class="w"> </span><span class="n">bx</span><span class="p">.</span><span class="n">tcx</span><span class="p">().</span><span class="n">lang_items</span><span class="p">().</span><span class="n">require</span><span class="p">(</span><span class="n">LangItem</span>::<span class="n">ExchangeMalloc</span><span class="p">)</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                    </span><span class="nb">Ok</span><span class="p">(</span><span class="n">id</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">id</span><span class="p">,</span><span class="w"></span>
<span class="w">                    </span><span class="nb">Err</span><span class="p">(</span><span class="n">s</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">                        </span><span class="n">bx</span><span class="p">.</span><span class="n">cx</span><span class="p">().</span><span class="n">sess</span><span class="p">().</span><span class="n">fatal</span><span class="p">(</span><span class="o">&amp;</span><span class="fm">format!</span><span class="p">(</span><span class="s">"allocation of `{}` {}"</span><span class="p">,</span><span class="w"> </span><span class="n">box_layout</span><span class="p">.</span><span class="n">ty</span><span class="p">,</span><span class="w"> </span><span class="n">s</span><span class="p">));</span><span class="w"></span>
<span class="w">                    </span><span class="p">}</span><span class="w"></span>
<span class="w">                </span><span class="p">};</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">instance</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">ty</span>::<span class="n">Instance</span>::<span class="n">mono</span><span class="p">(</span><span class="n">bx</span><span class="p">.</span><span class="n">tcx</span><span class="p">(),</span><span class="w"> </span><span class="n">def_id</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">r</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bx</span><span class="p">.</span><span class="n">cx</span><span class="p">().</span><span class="n">get_fn_addr</span><span class="p">(</span><span class="n">instance</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">call</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bx</span><span class="p">.</span><span class="n">call</span><span class="p">(</span><span class="n">r</span><span class="p">,</span><span class="w"> </span><span class="o">&amp;</span><span class="p">[</span><span class="n">llsize</span><span class="p">,</span><span class="w"> </span><span class="n">llalign</span><span class="p">],</span><span class="w"> </span><span class="nb">None</span><span class="p">);</span><span class="w"></span>
<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">val</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">bx</span><span class="p">.</span><span class="n">pointercast</span><span class="p">(</span><span class="n">call</span><span class="p">,</span><span class="w"> </span><span class="n">llty_ptr</span><span class="p">);</span><span class="w"></span>


<span class="w">                </span><span class="kd">let</span><span class="w"> </span><span class="n">operand</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">OperandRef</span><span class="w"> </span><span class="p">{</span><span class="w"> </span><span class="n">val</span>: <span class="nc">OperandValue</span>::<span class="n">Immediate</span><span class="p">(</span><span class="n">val</span><span class="p">),</span><span class="w"> </span><span class="n">layout</span>: <span class="nc">box_layout</span><span class="w"> </span><span class="p">};</span><span class="w"></span>
<span class="w">                </span><span class="p">(</span><span class="n">bx</span><span class="p">,</span><span class="w"> </span><span class="n">operand</span><span class="p">)</span><span class="w"></span>
<span class="w">            </span><span class="p">}</span><span class="w"></span>
</code></pre></div>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>